package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 1.继承HttpServlet
 * 2.重写doGet/doPost方法
 * 3.添加@WebServlet("/控制器名字")
 */
@WebServlet(urlPatterns = {"/hello","/hi","/helloWorld"})
public class HelloServlet extends HttpServlet {

    public HelloServlet() {
        System.out.println("1.servlet被实例化");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("2.servlet被初始化");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req,resp);
        System.out.println("3.执行了service方法...");
    }

    //    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter out = resp.getWriter();
//        out.print("<h1>Hello World!</h1>");
//        out.flush();
//        out.close();
//        System.out.println("3.执行了doGet方法...");
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("3.执行了doPost方法...");
//    }


    @Override
    public void destroy() {
        System.out.println("4.servlet被销毁");
    }
}
